<%-- 
    Document   : view_karyawan
    Created on : Jul 27, 2012, 5:50:37 PM
    Author     : MiChi
--%>

<%@page import="dao.AlokasiDAOImpl"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.KaryawanDAOImpl"%>
<%@page import="entity.Karyawan"%>
<%@page import="java.util.List"%>
<%@page import="additional.Help"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="javax.persistence.Persistence"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Karyawan</title>

        <link href="style/style.css" rel="stylesheet" type="text/css" /> 
        <script type="text/javascript">
            <%
                if (request.getParameter("delete") != null) {
                    try {
                        Long id = Long.parseLong(request.getParameter("delete"));
                        Karyawan k = new KaryawanDAOImpl(Help.getEM()).get(id);

                        if (k != null) {
                            out.write("if(confirm('Hapus Karyawan beserta Alokasinya?')) { \n"
                                    + "window.location='HapusKaryawan?id=" + k.getId() + "'; \n"
                                    + "} else { \n"
                                    + "window.location.replace('view_karyawan.jsp'); \n"
                                    + "}");
                        } else {
                            out.write("alert('Karyawan Tidak Ditemukan'); \n");
                            out.write("window.location.replace('view_karyawan.jsp');");
                        }
                    } catch (Exception e) {
                        out.write("alert('Terjadi Kesalahan'); \n");
                        out.write("window.location.replace('view_karyawan.jsp');");
                    }
                }
            %>
        </script>
    </head>
    <body>
        <div id="central">
            <div id="menuatas">
                <marquee>
                    <h3> Welcome </h3>
                </marquee>
            </div>

            <div id="header">
                <%
                    boolean isLogged = false;
                    if (request.getSession().getAttribute("user") != null) {
                        out.write("<a href='Logout'>LogOut</a>");
                        isLogged = true;
                    } else {
                        out.write("<a href='login.jsp'>LogIn</a>");
                    }
                %> 
            </div>

            <ul id="navlist">
                <li><a id="n1" href="index.jsp"><span>Home</span></a></li>
                <li><a id="n2" href="view_karyawan.jsp"><span>Karyawan</span></a></li>
                <li><a id="n3" href="view_pekerjaan.jsp"><span>Pekerjaan</span></a></li>
                <li><a id="n4" href="view_alokasi.jsp"><span>Alokasi Pekerjaan</span></a></li>                               
                <li><a id="n5" href="about.jsp"><span>About</span></a></li>                               
            </ul>
            <div id="content">                
                <div id="rightside">
                    <h1>Data Karyawan</h1>
                    <form method="get" action="view_karyawan.jsp">
                        <p>Nama Karyawan
                            <input type="text" name="txtCari" value=""> 
                            <input type="submit" value="Search">
                        </p>
                    </form>
                    <hr> 
                    <table border="0" width="100%">
                        <tr>
                            <th>ID</th>
                            <th>Nama</th>
                            <th>No Identitas</th>
                            <th>No Telepon</th>
                            <th>Alamat</th>
                        </tr>
                        <%
                            try {
                                EntityManager em = Help.getEM();
                                List<Karyawan> listKary = new ArrayList<Karyawan>();
                                if (request.getParameter("txtCari") != null) {
                                    String nama = request.getParameter("txtCari");
                                    listKary = new KaryawanDAOImpl(em).findNama(nama);
                                } else {
                                    listKary = new KaryawanDAOImpl(em).gets();
                                }

                                for (Karyawan k : listKary) {
                                    out.write("<tr> \n");
                                    out.write("<td>" + k.getId() + "</td> \n");
                                    out.write("<td>" + k.getNama() + "</td> \n");
                                    out.write("<td>" + k.getNoidentitas() + "</td> \n");
                                    out.write("<td>" + k.getNotelp() + "</td> \n");
                                    out.write("<td>" + k.getAlamat() + "</td> \n");
                                    // out.write("<td> <a href='detail_pekerjaan.jsp?id=" + k.getId() + "'>Detail</a> </td> \n");
                                    if (isLogged) {
                                        out.write("<td> <a href='view_karyawan.jsp?delete=" + k.getId() + "'>Hapus</a></td>");
                                        out.write("<td> <a href='edit_karyawan.jsp?id=" + k.getId() + "'>Edit</a></td>");
                                    }
                                    out.write("</tr> \n");
                                }
                            } catch (Exception e) {
                            }
                        %>
                    </table>            
                    
                    <br>
                    <%
                        if (isLogged) {
                            out.write("<a href='add_karyawan.jsp'>Tambah Karyawan</a>");
                        }
                    %>                    
                </div>
                <div id="footer">
                    <p>&COPY; copyright TR PERSISTENCE</p>
                </div>
            </div>
        </div>
    </body>
</html>
